package com.adm.biz;

import java.util.List;
import java.util.Map;

import org.yfsoft.comm.util.BizResult;

import com.adm.ctl.ResultType;
import com.adm.dao.CommonDao;

public class TplService extends Service {

	private CommonDao dao;
	@Override
	public void setDao(CommonDao dao) {
		this.dao = dao;
	}
	
	
	public BizResult list(Map<String,String> input){
		String m_id = input.get("m_id");
		Object m = this.dao.query("sys_model",null,"m_id = "+m_id).getData();
		this.setReqAttr("model", m);
		BizResult rst = this.dao.injoin(new String[]{"sys_model_control mc","sys_control c"}, 
				new String[]{"mc.*","c.*"},
				"mc.mc_control_id = c.c_id",
				"mc.mc_model_id = "+m_id,
				null,100,0);
		if(((List)rst.getData()).size()<1){
			this.setReqAttr("msg", "未设置栏位信息");
			rst.setType(String.valueOf(ResultType.REDIRECT));
			rst.setPage("tpl/list.jsp");
			return rst;
		}
		this.setReqAttr("controls", rst.getData());
		Object entrys = this.dao.list(((Map<String,Object>)m).get("m_view").toString(),null,null,null,100,0).getData();
		this.setReqAttr("es", entrys);
		rst.setType(String.valueOf(ResultType.REDIRECT));
		rst.setPage("tpl/list.jsp");
		return rst;
	}
	
	public BizResult form(Map<String,String> input){
		String m_id = input.get("m_id");
		Object m = this.dao.query("sys_model",null,"m_id = "+m_id).getData();
		this.setReqAttr("model", m);
		BizResult rst = this.dao.injoin(new String[]{"sys_model_control mc","sys_control c"}, 
				new String[]{"mc.*","c.*"},
				"mc.mc_control_id = c.c_id",
				"mc.mc_model_id = "+m_id,
				null,100,0);
		this.setReqAttr("controls", rst.getData());
		String p =input.get("p");
		if("add".equals(p)){
			//添加表单
			this.setReqAttr("p", "add");
		}else if("edit".equals(p)){
			//修改表单
			//TODO..加载属性
			String e_id = input.get("key");
			String pri = input.get("pri");
			String v = input.get("v");
			this.setReqAttr("e", this.dao.query(v,null,pri+"="+e_id).getData());
			this.setReqAttr("p", "edit");
			this.setReqAttr("pri", pri);
			this.setReqAttr("key", e_id);
			this.setReqAttr("v",v);
		}
		rst.setType(String.valueOf(ResultType.REDIRECT));
		rst.setPage("tpl/form.jsp");
		return rst;
	}
	
	public BizResult delete(Map<String,String> input){
		String key = input.get("key");
		String pri = input.get("pri");
		BizResult rst = this.dao.delete(input.get("v"),pri+"="+key);
		if("0".equals(rst.getCode())){
			this.setReqAttr("msg", "OK");
		}else{
			this.setReqAttr("msg", rst.getMsg());
		}
		return list(input);
	}
	
	public BizResult edit(Map<String,String> input){
		String key = input.get("key");
		String pri = input.get("pri");
		String v = input.get("v");
		BizResult rst = this.dao.update(input, pri+"="+key, v);
		if("0".equals(rst.getCode())){
			this.setReqAttr("msg", "OK");
		}else{
			this.setReqAttr("msg", rst.getMsg());
		}
		return list(input);
	}
	
	public BizResult add(Map<String,String> input){
		String m_id = input.get("m_id");
		input.remove("m_id");
		Object m = this.dao.query("sys_model",null,"m_id = "+m_id).getData();
		Map<String,String> model = (Map<String,String>)m;
		this.setReqAttr("model", m);
		this.dao.insert(input,model.get("m_view") );
		input.put("m_id", m_id);
		return list(input);
	}

}
